{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e211cfe",
   "metadata": {},
   "source": [
    "---------------\n",
    "* 日期: 2022-10-19\\week09\n",
    "* 课程： python\n",
    "* 记录人：姚远航\n",
    "---------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05656752",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1.列表的方法   \n",
    "\n",
    "> 2. while循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e60cba15",
   "metadata": {},
   "source": [
    "## 1.列表的方法\n",
    "> 1.使用.Tab键查看列表的方法    \n",
    "> 2.使用help(查看方法的使用规则)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d83b441d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = ['Jack','Alex','Marry','Jam'] # 列表里面放字符串\n",
    "names\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0be8689f",
   "metadata": {},
   "source": [
    "## 1.1列表的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dddcad27",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "83145179",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b730a704",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "71b759ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c741ba31",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"yyy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "79ee300a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'yyy', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5409d542",
   "metadata": {},
   "source": [
    "### 2. 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ba6b8aba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "064c8ba1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"yyy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8af0ad94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49766064",
   "metadata": {},
   "source": [
    "* 正确的删除方法的流程\n",
    "> 1. 查询value是否存在列表中\n",
    "> 2. 对存在的value进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3984644a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"wang\" in names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f31bd06d",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"wang\" in names:\n",
    "    names.remove(\"wang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5222e449",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践： 删除系统：1.如果输入的值存在，删除；2.如果输入的结果不存在，提示用户查询无结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7ba40763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e48589f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入要删除内容：Jack\n",
      "删除成功\n",
      "['Alex', 'Marry', 'Jam']\n"
     ]
    }
   ],
   "source": [
    "user_input_name = input(\"请输入要删除内容：\")\n",
    "if user_input_name in names:\n",
    "    names.remove(user_input_name)\n",
    "    print(\"删除成功\")\n",
    "else:\n",
    "    print(\"输入的结果不存在\")\n",
    "print(names)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d15b9ecd",
   "metadata": {},
   "source": [
    "------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a1afbaf",
   "metadata": {},
   "source": [
    "* 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2b61ee1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新建立names列表数据\n",
    "names = ['Jack','Alex','Marry','Jam','lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b398f49e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "44c6f5b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5987b0af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ea5e9a9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lihua'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop() #直接使用pop，删除最后一位。（append是增加最后一位）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c0a69302",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "49d1fa32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f615c8e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Jam']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8c6fdc9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "aa537894",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入删除的索引值：2\n"
     ]
    }
   ],
   "source": [
    "pop_index = int(input(\"输入删除的索引值：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "376fe96d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除成功！\n"
     ]
    }
   ],
   "source": [
    "if pop_index<len(names):\n",
    "    names.pop(pop_index)\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"您输入的值不在index的范围内\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "65949682",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9dbe8c0",
   "metadata": {},
   "source": [
    "### 3.列表的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ebeba6ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "names[0] =\"yyy\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "92d3cfe7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['yyy', 'Alex', 'Jam']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9444b6ce",
   "metadata": {},
   "source": [
    "### 4.列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "7a6d7174",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "da34d36a",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "318da521",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "5e4a3d90",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1 = ['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "946df4ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b899e71e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "4683299e",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class2 =[\"李晓峰\",\"王丽\",\"刘明\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "ccfa232b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李晓峰', '王丽', '刘明']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a9e88e80",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1.extend(names_class2) #注意：使用列表的方法的列表进行扩展"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9815ba89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Alex', 'Marry', 'Jam', 'lihua', '李晓峰', '王丽', '刘明']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba5eb0f1",
   "metadata": {},
   "source": [
    "-----\n",
    "* sort()排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3825e822",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生 = [230,250,245,255,260,270]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "57698dbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(立定跳远_男生.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "74a37375",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "2f9671a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[230, 245, 250, 255, 260, 270]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "c9627b2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort(reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "82591b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[270, 260, 255, 250, 245, 230]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90ae4235",
   "metadata": {},
   "source": [
    "# 2.while循环\n",
    "* while True 是循环概念在非常重要的一种方式，它可以实现for循环的指定次数，也可以在特定条件下停止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "a9ad545f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯... 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 9\n"
     ]
    }
   ],
   "source": [
    "# 死循环\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯...\",count)\n",
    "    count = count+1\n",
    "    if count == 10:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a508065a",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 小游戏：猜数字小游戏\n",
    "# 1. hide_card :0-100,大了，小了\n",
    "# 2. guess_digit ：50\n",
    "count =0\n",
    "hide_card= int(input(\"hide_card：\"))\n",
    "\n",
    "guess_digit =int(input(\"猜一猜 (0-100) ：\"))\n",
    "# 对比：\n",
    "if hide_card=guess_digit：\n",
    "    print()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f877b020",
   "metadata": {},
   "outputs": [],
   "source": [
    "#课后练习：\n",
    "> 1. 列表的切片和方法的使用\n",
    ">> 1.课本p67\n",
    ">> 2. 课本p68"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
